package main

import "fmt"

type Tag struct {
	ID   int64 // tag的主键
	Name string
}

type Operation struct {
	Add []Tag // 新增的tag
	Del []Tag // 删除的tag
}

func Process(base []Tag, op Operation) []Tag {
	baseTag := make(map[int64]Tag)
	order := []int64{}
	for _, b := range base {
		baseTag[b.ID] = b
		order = append(order, b.ID)
	}

	for _, a := range op.Add {
		if _, exists := baseTag[a.ID]; exists {
			baseTag[a.ID] = a
		} else {
			baseTag[a.ID] = a
			order = append(order, a.ID)
		}
	}

	for _, d := range op.Del {
		delete(baseTag, d.ID)
		for i, id := range order {
			if id == d.ID {
				copy(order[i:], order[i+1:])
				order = order[:len(order)-1]
				break
			}
		}
	}

	newTag := make([]Tag, 0, len(baseTag))
	for _, id := range order {
		newTag = append(newTag, baseTag[id])
	}
	return newTag
}

func main() {
	baseTags := []Tag{
		{ID: 1, Name: "a"},
		{ID: 4, Name: "b"},
		{ID: 3, Name: "c"},
	}

	// 创建操作列表
	op := Operation{
		Add: []Tag{
			{ID: 2, Name: "d"},
			{ID: 1, Name: "e"},
		},
		Del: []Tag{
			{ID: 3, Name: "c"},
		},
	}
	for i := 0; i < 10; i++ {
		processedTags := Process(baseTags, op)
		fmt.Println(processedTags)
	}

}
